﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using BYFW.DAL;
using System.Data;
using LDSP.Util;
using LDSP.Constants;
using System.Drawing;
using LDSP.Helper;

namespace LDSP.UI.Admin
{
    public partial class MngExposureDraft : LDSP.BRL.BaseValidUserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    this.CheckQS();
                }
                if (this.RB())
                {
                    this.InitDataDynamicBefore();
                    if (!IsPostBack)
                    {
                        this.InitData();
                        this.BindGrid();
                    }
                    this.InitDataDynamicAfter();
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        private int col_chk = 0;
        private int col_index = 1;
        private int col_name = 2;
        private int col_org = 3;
        private int col_reference_number = 4;
        private int col_release_DT = 5;
        private int col_end_DT = 6;
        private int col_summary = 7;
        private int col_isactive = 8;
        private int col_op = 9;

        protected string TitleName = "征求意见稿";

        protected int LastID
        {
            get { return this.GetVSInt("LastID"); }
            set
            {
                this.SetVS("LastID", value);
            }
        }

        protected string EmptySearchStr
        {
            get { return this.GetVSString("EmptySearchStr"); }
            set { this.SetVS("EmptySearchStr", value); }
        }

        /// <summary>
        /// 传入参数，设置过滤条件
        /// </summary>
        protected void SetSpecialPageInfo()
        {
            g_pageInfo.Direction = false;
            g_pageInfo.Sort = "releaseDT";
            g_pageInfo.PageSize = GetPageSizeSetting();
            g_pageInfo.PageIndex = this.grd_list.PageIndex;
        }
        protected void CheckQS()
        {
        }
        protected bool RB()
        {
            return true;
        }
        protected void InitData()
        {
            //不缓存则清空页面信息
            if (!needCachePageInfo)
            {
                g_pageInfo.ClearPageInfo();
            }

            EmptySearchStr = Constants.EmptySearch.ExposureDraft;
            UIHelper.SetDefaultFilterCondition(this.txt_name, EmptySearchStr);

            this.GetCondition();
            this.SetSpecialPageInfo();
            this.BindDDLPageSize();
            this.JSShowModalDialog("cid=" + LastID, this.btn_add);
            this.JSShowModalDialog("cid=" + LastID, this.btn_Add1);
        }
        protected void InitDataDynamicBefore()
        {
            //根据类型不同，设置相应的label和grid的header text.
        }
        protected void InitDataDynamicAfter()
        {
            this.JSSetDateTimeReadOnly(this.txt_releaseStartDT.ClientID);
            this.JSSetDateTimeReadOnly(this.txt_releaseEndDT.ClientID);
            this.JSSetDateTimeReadOnly(this.txt_endStartDT.ClientID);
            this.JSSetDateTimeReadOnly(this.txt_endEndDT.ClientID);

            //如果是企业用户，则隐藏添加/删除按钮
            if (this.LoginAdmin == null)
            {
                this.grd_list.Columns[col_isactive].Visible = false;

                this.btn_add.Visible = false;
                this.btn_Add1.Visible = false;
                this.btn_delete.Visible = false;
                this.btn_Delete1.Visible = false;
            }

            UIHelper.SetDefaultFilterCondition(this.txt_name, EmptySearchStr, false);
        }
        private void BindDDLPageSize()
        {
            CommonControlInitial.InitialPageSize(this.ddl_pagesize);
            this.ddl_pagesize.SelectedValue = g_pageInfo.PageSize.ToString();
        }
        protected void BindGrid()
        {
            //更新关键字表,以便智能提示.
            string keyword = this.txt_name.Text.Trim();
            if (EmptySearchStr.Equals(keyword))
            {
                keyword = "";
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                new SearchKeywords().IncreaseWordsCount(keyword);
            }

            DataTable dt = null;

            this.grd_list.PageSize = g_pageInfo.PageSize;
            this.grd_list.PageIndex = g_pageInfo.PageIndex;

            dt = BYFW.SFL.CommonOP.SortedTable(new ExposureDraft().GetRecords(g_pageInfo.ConditionWhere), g_pageInfo.GetSortExpression());
            this.grd_list.DataSource = dt;
            this.grd_list.DataBind();

            SavePageSizeSetting();

            this.TotalRecords.Text = LDSP.Helper.StringUtil.GetSearchText(dt.Rows.Count);
        }
        protected void GetCondition()
        {
            string cond = "";
            string nameCond = this.txt_name.Text.Trim();
            if (nameCond != "" && nameCond != EmptySearchStr)
            {
                cond = this.AppendConditionMulti(cond, this.txt_name.Text.Trim(), "name", "Organization", "Summary", "ReferenceNumber");
            }
            this.SetHighlight(this.txt_name.Text.Trim());
            if (this.txt_releaseStartDT.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("releaseDT", EDataType.Date, this.txt_releaseStartDT.Text, ">=", true), true);
            }
            if (this.txt_releaseEndDT.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("releaseDT", EDataType.Date, this.txt_releaseEndDT.Text, "<=", true), true);
            }
            if (this.txt_endStartDT.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("endDT", EDataType.Date, this.txt_endStartDT.Text, ">=", true), true);
            }
            if (this.txt_endEndDT.Text.Trim() != "")
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("endDT", EDataType.Date, this.txt_endEndDT.Text, "<=", true), true);
            }

            //对于企业用户，只能看到未失效的征求意见稿
            if (this.LoginAdmin == null)
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("isactive", EDataType.Bool, "True", "=", true), true);
            }

            g_pageInfo.Condition = cond;
        }
        private void JSShowModalDialog(string arg, WebControl ctrl)
        {
            this.JSShowDialog("../Admin/AddExposureDraft.aspx?" + arg, 700, 500, ctrl, null, null, null, null, null, null, null, true, null, null, null, null, null);
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_delete_Click(object sender, EventArgs e)
        {
            try
            {
                this.DeleteCheckedInGrid<ExposureDraft>(this.grd_list);
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_search_Click(object sender, EventArgs e)
        {
            try
            {
                this.GetCondition();
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected void grd_list_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int id = this.GetInt(e.CommandArgument.ToString());
                ExposureDraft ett = new ExposureDraft();
                ett.GetProperties(id);
                if (e.CommandName == "downloadItem")
                {
                    this.StartDownload(ett.ID, DocType.征求意见稿);
                }
                else if (e.CommandName == "deleteItem")
                {
                    ett.Delete();
                }
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    ExposureDraft ett = new ExposureDraft();
                    ett.GetProperties(((DataRowView)e.Row.DataItem).Row);

                    //设置序号
                    this.ShowSequenceNumber(e.Row, 1);

                    //将"修改"按钮与修改页面绑定
                    WebControl c = (WebControl)e.Row.FindControl("LinkButton2");
                    this.JSShowModalDialog("id=" + ett.ID, c);

                    //如果是企业用户，隐藏"修改"和"删除"按钮
                    if (LoginAdmin == null)
                    {
                        WebControl lnk_Modify = (WebControl)e.Row.FindControl("LinkButton2");
                        WebControl lnk_Delete = (WebControl)e.Row.FindControl("LinkButton3");
                        lnk_Modify.Visible = false;
                        lnk_Delete.Visible = false;
                    }

                    //检查当前item是否在选中列表里
                    CheckBox chb = (CheckBox)e.Row.FindControl("chb");
                    chb.Attributes["docid"] = ett.ID.ToString();
                    if (this.MultiPageIncludeCheckedId(this.hid_docIDs, ett.ID))
                    {
                        chb.Checked = true;
                    }

                    //设定格式
                    e.Row.Cells[col_release_DT].Text = this.FormatDT(ett.ReleaseDT);
                    e.Row.Cells[col_end_DT].Text = this.FormatDT(ett.EndDT);
                    //e.Row.Cells[col_upload_DT].Text = this.FormatDT(ett.UploadDT);
                    e.Row.Cells[col_summary].Text = ett.Summary.Replace("\n", "</br>");
                    e.Row.Cells[col_isactive].Text = ett.IsActive == true ? "是" : "否";

                    UIHelper.AppendEmptyLine(e.Row, col_summary);

                    //高亮显示搜索关键字
                    this.HighlightMulti(e.Row, col_name, col_org, col_summary);
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                g_pageInfo.PageIndex = e.NewPageIndex;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                if (g_pageInfo.Sort == e.SortExpression)
                {
                    g_pageInfo.Direction = !g_pageInfo.Direction;
                }
                else
                {
                    g_pageInfo.Direction = true;
                }
                g_pageInfo.Sort = e.SortExpression;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void ddl_pageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                g_pageInfo.PageSize = this.GetInt(this.ddl_pagesize.SelectedValue);
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
    }
}